Skip to content

Fix COPY cleanup when a records generator raises - #1358

Open
CodingCossack wants to merge 1 commit into
MagicStack:masterfrom
CodingCossack:fix-copy-generator-error
Open

Fix COPY cleanup when a records generator raises#1358
CodingCossack wants to merge 1 commit into
MagicStack:masterfrom
CodingCossack:fix-copy-generator-error

Conversation

@CodingCossack

Copy link
Copy Markdown

Fixes #1354.

When a records generator raises, COPY cleanup currently sends str(e) as the CopyFail reason. PostgreSQL limits the message to 10,000 bytes, including the four-byte length field and trailing NUL. A reason longer than 9,995 encoded bytes therefore produces an invalid message, closes the connection and can leave transaction rollback waiting indefinitely.

Use the fixed ASCII reason COPY source failed instead. This avoids oversized messages and secondary failures while formatting or encoding exception text. Keep the existing cancellation flow and bare re-raise, preserving the original application exception on this path. PostgreSQL now receives a generic reason; the caller's exception is not truncated or replaced.

Regression coverage exercises synchronous and asynchronous generators, implicit and explicit transactions, ASCII and multibyte byte-length boundaries, failing __str__, and unencodable exception text. Each case sends COPY data before failing, checks exception identity and rollback/atomicity, then performs a successful COPY and query on the same connection and closes it normally.

Validation

Tested locally on macOS arm64 with CPython 3.13.14 and PostgreSQL 16.14:

  • New regressions fail with the unchanged production code and pass after the fix.
  • python -m unittest -v tests.suite: successful; 313 tests run, four skipped, including the suite's flake8 and mypy checks.
  • USE_UVLOOP=1 python -m unittest -v tests.test_copy: 26 tests passed.
  • New tests also pass with PYTHONASYNCIODEBUG=1; git diff --check passes.

This change does not attempt a general connection-loss/cancellation-state repair. The full upstream CI matrix was not run locally.

AI assistance

OpenAI Codex was used for implementation and local test execution. ChatGPT was used for independent source/patch review and preparation of this description.

Use a fixed short CopyFail reason instead of formatting the application
exception. PostgreSQL limits this message to 10000 bytes, including its
length field and trailing NUL. An oversized reason loses protocol
synchronisation and can leave transaction rollback waiting indefinitely.

Keep the original application exception intact, including when formatting
or encoding it would fail. Cover encoded-byte boundaries, multibyte text,
sync and async generators, COPY atomicity, rollback and connection reuse.

Fixes MagicStack#1354
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Connection.copy_records_to_table hanging when generator passed to records parameter raises exception with long error message

1 participant